package me.zhenqu.web.controller;

import me.zhenqu.model.pojo.entity.Status;
import me.zhenqu.model.pojo.entity.User;
import me.zhenqu.services.StatusService;
import me.zhenqu.services.UserService;
import me.zhenqu.web.bvo.CountedStatus;
import me.zhenqu.web.bvo.SearchedUser;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: yibing.tan
 * Date: 11-11-17
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
public class ObjectWraper {
    public static List<CountedStatus> wrap(List<Status> statuses, StatusService statusService) {
        List<CountedStatus> countedStatusList = new LinkedList<CountedStatus>();
        for (Status current:statuses){
            CountedStatus cs = wrap(current, statusService);
            countedStatusList.add(cs);
        }
        return countedStatusList;
    }

    public static CountedStatus wrap(Status current, StatusService statusService){
        CountedStatus cs = new CountedStatus();
        cs.setStatus(current);
        cs.setCount(statusService.getStatusCount(current.getStatus_id()));
        return cs;
    }

    public static List<SearchedUser> wrapUser(Long currentUid, List<User> searchedUsers, UserService userService) {
        List<SearchedUser> countedStatusList = new LinkedList<SearchedUser>();
        for (User current:searchedUsers){
            if (current.getUser_id()==currentUid) continue;
            SearchedUser cs = wrapUser(currentUid, current, userService);
            countedStatusList.add(cs);
        }
        return countedStatusList;
    }

    private static SearchedUser wrapUser(Long currentUid, User user, UserService userService) {
        boolean following = userService.isFollowing(user.getUser_id(), currentUid);
        SearchedUser searchedUser = new SearchedUser();
        searchedUser.setUser(user);
        if (!following)
            searchedUser.setNeedFollow(true);
        return searchedUser;
    }
}
